﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows;
using System.Reflection;
using InteControlSys.ViewModel;
using System.ComponentModel;
using System.Windows.Documents;
using Microsoft.Win32;
using System.Windows.Controls;
using System.Threading;
using InteControlSys.View;

namespace InteControlSys.ViewModel
{
    public class StartWindowViewModel : INPCBase
    {
        public StartWindow startWindow;

        private string notice1;
        private string notice2;
        private string notice3;
        //private List<User> userList;
        public string Notice1
        {
            get { return notice1; }
            set { notice1 = value; NotifyChanged("Notice1"); }
        }
        public string Notice2
        {
            get { return notice2; }
            set { notice2 = value; NotifyChanged("Notice2"); }
        }
        public string Notice3
        {
            get { return notice3; }
            set { notice3 = value; NotifyChanged("Notice3"); }
        }
        public String UserName { get; set; }
        public String Password { get; set; }
        #region command

        public SimpleCommand LogonCommand { get; set; }
        public SimpleCommand CancelCommand { get; set; }
        public SimpleCommand RegistCommand { get; set; }

        #endregion

        #region method for command

        MainWindow mainWin;
        private void Logon(object parameter)
        {
            //this.Password = ((PasswordBox)parameter).Password.ToString();
            ////登录判断
            //if (this.UserName == null||this.UserName == "")
            //{
            //    this.Notice1 = "";
            //    this.Notice2 = "*用户名不为空";
            //    this.Notice3 = "";
            //    return;
            //}
            //if (this.Password == null || this.Password == "")
            //{
            //    this.Notice1 = "";
            //    this.Notice2 = "";
            //    this.Notice3 = "*密码不为空";
            //    return;
            //}

            //bool success = false;
            //foreach(User item in userList)
            //{
            //    if(item.UserName == this.UserName && item.Password == this.Password)
            //    {
            //        success = true;
            //        break;
            //    }
            //}

            //if(!success)
            //{
            //    this.Notice1 = "*用户名或密码错误";
            //    this.Notice2 = "";
            //    this.Notice3 = "";
            //    return;
            //}
            MainWindow.OperateNo = this.UserName;

            var mainWin = new MainWindow();
            
            mainWin.Show();
            this.startWindow.Close();
        }
        private void Cancel(object parameter)
        {
            this.startWindow.Close();
            //this.mainWin.Close();
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }
        private void Regist(object parameter)
        {
            ////this.startWindow.Close();

            //this.Password = ((PasswordBox)parameter).Password.ToString();
            //if (this.UserName == null || this.UserName == "")
            //{
            //    this.Notice1 = "";
            //    this.Notice2 = "*用户名不为空";
            //    this.Notice3 = "";
            //    return;
            //}
            //if (this.Password == null || this.Password == "")
            //{
            //    this.Notice1 = "";
            //    this.Notice2 = "";
            //    this.Notice3 = "*密码不为空";
            //    return;
            //}

            //bool success = false;
            //foreach (User item in userList)
            //{
            //    if (item.UserName == this.UserName)
            //    {
            //        success = true;
            //        break;
            //    }
            //}
            //if (success)
            //{
            //    this.Notice1 = "*该用户名已存在";
            //    this.Notice2 = "";
            //    this.Notice3 = "";
            //    return;
            //}

            //User user = new User();
            //user.Id = 7;
            //user.UserName = this.UserName;
            //user.Password = this.Password;
            //ManagerBase<User> manager = new ManagerBase<User>();
            //if (manager.Save(user))
            //{
            //    MessageBox.Show("注册成功，请登录！");
            //    this.startWindow.LogOnBtn_Click(null, null);
            //    this.userList = new ManagerBase<User>().RetrieveAll().ToList(); 
            //}

        }

        #endregion

        public delegate void DeleFunc();
        public StartWindowViewModel()
        {
            this.InitializeCommands();

            try
            {
                //this.userList = new ManagerBase<User>().RetrieveAll().ToList();
                //var q = new ManagerBase<YardCount>();
                //var q2 = q.RetrieveAll();
                //IEnumerable<User> users = q2;
                //var q3 = q2.ToList();
                //this.userList = q3;

            }
            catch(Exception e)
            {
                MessageBox.Show(e.ToString());

                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }


            //mainWin = new MetroStyleWindow();

            //ThreadStart threadStart = new ThreadStart(OpenMainWindow);
            //Thread thread = new Thread(threadStart);
            //thread.Start();

            //Thread NetServer = new Thread(new ThreadStart(OpenMainWindow));
            //NetServer.SetApartmentState(ApartmentState.STA);
            //NetServer.IsBackground = true;
            //NetServer.Start();

            //System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
            //                                            new DeleFunc(OpenMainWindow));
        }

        public void OpenMainWindow()
        {
            //mainWin = new MetroStyleWindow();
        }
        private void InitializeCommands()
        {
            this.LogonCommand = new SimpleCommand(this.Logon);
            this.CancelCommand = new SimpleCommand(this.Cancel);
            //this.RegistCommand = new DelegateCommand<object>(new Action<object>(this.Regist));

        }
    }

}
